<?php
namespace Admin\Controller;
use Think\Controller;
class RecordController extends CommonController {

	protected $table = 'Record';

	/**
	 * 初始化
	 */
	public function __construct() {
		parent::__construct();
	}

	public function index() {
		if($_POST['id']) {
			$id = I('post.id','','intval');
			$time = I('post.time','','intval');
			$lwhere['lcid'] = $id;
			$lwhere['ltime'] = $time;
			$lesson = D('lesson')->where($lwhere)->find();
			if(!empty($lesson)) {
				header('Location:'.U('Record/index',array('id'=>$id,'time'=>$time)));
			}
			$msg = D('record')->where(array('rcid'=>$id,'rtime'=>$time))->delete();
			$idarr = $_POST['idl'];
			foreach($idarr as $value) {
				$data = array();
				$data['rcid'] = $id;
				$data['rtime'] = $time;
				$data['rstatus'] = 0;
				$data['rsid'] = $value;
				$msg = D('record')->add($data);
			}

			$this->success('操作成功！',U('Record/index',array('id'=>$id,'time'=>$time)));
			die();
		}

		$id = I('get.id','','intval');
		$this->assign('id',$id);

		$time = I('get.time','','intval');
		$this->assign('time',$time);
		
		$where = 'data_student on data_class_student.scid = '.$id.' and data_class_student.ssid = data_student.id';
		$this->pagejoin("class_student", $where, $order, 200); // 分页
		$this->assign('student',$student);

		$recordarr = D('record')->where(array('rcid'=>$id,'rtime'=>$time))->select();
		$record = array();
		foreach($recordarr as $value) {
			$record[] = $value['rsid'];
		}
		$this->assign('record',$record);

		$this->display();
	}
	
	/**
	 * 添加记录
	 */
	public function add() {
		
		if($_POST['id']) {
			$id = I('post.id','','intval');
			$time = I('post.time','','intval');
			$lwhere['lcid'] = $id;
			$lwhere['ltime'] = $time;
			$lesson = D('lesson')->where($lwhere)->find();
			if(!empty($lesson)) {
				header('Location:'.U('Record/index',array('id'=>$id,'time'=>$time)));
			}
			$msg = D('record')->where(array('rcid'=>$id,'rtime'=>$time))->delete();
			$idarr = $_POST['idl'];
			if(empty($idarr)) {
				$this->error('请选择学员！');
			}
			foreach($idarr as $value) {
				$data = array();
				$data['rcid'] = $id;
				$data['rtime'] = $time;
				$data['rstatus'] = 0;
				$data['rsid'] = $value;
				$msg = D('record')->add($data);
			}

			$datas['lcid'] = $id;
			$datas['ltime'] = $time;
			$msg = D('lesson')->add($datas); // 课程列表
			D('class')->where('cid='.$id)->setInc('crest');  // 课程进度
			

			$this->success('操作成功！',U('Record/index',array('id'=>$id,'time'=>$time)));
			die();
		}

		$id = I('get.id','','intval');
		$this->assign('id',$id);

		$time = I('get.time','','intval');
		$this->assign('time',$time);
		
		$where = 'data_student on data_class_student.scid = '.$id.' and data_class_student.ssid = data_student.id';
		$this->pagejoin("class_student", $where, $order, 200); // 分页
		$this->assign('student',$student);

		$this->display();
	}
	
}
